#include <caemutmtcserver/CAEmuTMTCServerE.h>

// ***********************************************************************

// class EDROOMTopContext

// ***********************************************************************



	// CONSTRUCTORES***********************************************

CAEmuTMTCServer::EDROOMTopContext::EDROOMTopContext (CAEmuTMTCServer &act, CDDataBurst & EDROOMpVarVDataBurst, CDFrame & EDROOMpVarVDataFrame, CPackListHandler & EDROOMpVarVEmuGSFree, CPackListHandler & EDROOMpVarVEmuGSTC, CPackListHandler & EDROOMpVarVFreePackList, int & EDROOMpVarVMessageNumber, int & EDROOMpVarVMessageReceived, bool_t & EDROOMpVarVMoreData, int & EDROOMpVarVnumMensajeTC, int & EDROOMpVarVnumMensajeTM, PackPool & EDROOMpVarVPackListPool, CPackListHandler & EDROOMpVarVTCPackList, CPackListHandler & EDROOMpVarVTMPackList, CEDROOMPOOLCDTMTCCommServer & EDROOMpPoolCDTMTCCommServer, CEDROOMPOOLCDFrame & EDROOMpPoolCDFrame, CEDROOMPOOLCDSignal & EDROOMpPoolCDSignal ):

	EDROOMcomponent(act)
	,Msg(EDROOMcomponent.Msg)
	,MsgBack(EDROOMcomponent.MsgBack)
	,StoreAndNextFrame(EDROOMcomponent.StoreAndNextFrame)
	,SgiTMTCServer2(EDROOMcomponent.SgiTMTCServer2)
	,SgiTMTCServer(EDROOMcomponent.SgiTMTCServer)
	, VDataBurst ( EDROOMpVarVDataBurst )
	, VDataFrame ( EDROOMpVarVDataFrame )
	, VEmuGSFree ( EDROOMpVarVEmuGSFree )
	, VEmuGSTC ( EDROOMpVarVEmuGSTC )
	, VFreePackList ( EDROOMpVarVFreePackList )
	, VMessageNumber ( EDROOMpVarVMessageNumber )
	, VMessageReceived ( EDROOMpVarVMessageReceived )
	, VMoreData ( EDROOMpVarVMoreData )
	, VnumMensajeTC ( EDROOMpVarVnumMensajeTC )
	, VnumMensajeTM ( EDROOMpVarVnumMensajeTM )
	, VPackListPool ( EDROOMpVarVPackListPool )
	, VTCPackList ( EDROOMpVarVTCPackList )
	, VTMPackList ( EDROOMpVarVTMPackList )
	, CEDROOMPoolCDTmTcCommServer ( EDROOMpPoolCDTMTCCommServer )
	, EDROOMPoolCDFrame ( EDROOMpPoolCDFrame )
	, EDROOMPoolCDSignal ( EDROOMpPoolCDSignal )
 {

}

CAEmuTMTCServer::EDROOMTopContext::EDROOMTopContext ( EDROOMTopContext &contex ):

	EDROOMcomponent(contex.EDROOMcomponent),
	Msg(contex.Msg),
	MsgBack(contex.MsgBack)
	,StoreAndNextFrame(contex.StoreAndNextFrame)
	,SgiTMTCServer2(contex.SgiTMTCServer2)
	,SgiTMTCServer(contex.SgiTMTCServer)
	, VDataBurst ( contex.VDataBurst )
	, VDataFrame ( contex.VDataFrame )
	, VEmuGSFree ( contex.VEmuGSFree )
	, VEmuGSTC ( contex.VEmuGSTC )
	, VFreePackList ( contex.VFreePackList )
	, VMessageNumber ( contex.VMessageNumber )
	, VMessageReceived ( contex.VMessageReceived )
	, VMoreData ( contex.VMoreData )
	, VnumMensajeTC ( contex.VnumMensajeTC )
	, VnumMensajeTM ( contex.VnumMensajeTM )
	, VPackListPool ( contex.VPackListPool )
	, VTCPackList ( contex.VTCPackList )
	, VTMPackList ( contex.VTMPackList )
	, CEDROOMPoolCDTmTcCommServer ( contex.CEDROOMPoolCDTmTcCommServer )
	, EDROOMPoolCDFrame ( contex.EDROOMPoolCDFrame )
	, EDROOMPoolCDSignal ( contex.EDROOMPoolCDSignal )
 {

}

	// EDROOMSearchContextTrans********************************************

bool CAEmuTMTCServer::EDROOMTopContext::EDROOMSearchContextTrans(TEDROOMTransId &edroomCurrentTrans){

	bool edroomValidMsg=false; 

	 switch(Msg->signal){

		 case ( EDROOMSignalDestroy ): 

			 edroomValidMsg=true;
			 edroomCurrentTrans.distanceToContext = 0 ;
			 edroomCurrentTrans.localId = -1 ;
			 break;

	}

	return(edroomValidMsg);

}

	// Funciones Definidas por el usuario  ****************************

void	CAEmuTMTCServer::EDROOMTopContext::FCheckFiles()

 
{
	FILE *originFile,*finalFile;
	bool_t ok=true;
	byte_t originBuffer[50], finalBuffer[50];
 
   finalFile=fopen("InterTM.jpg","rb");
   originFile=fopen("OUpFrames.jpg","rb");
   do
	{
	   for (TEDROOMInt32 i =0; i< 50; i++)
	   {
		   originBuffer[i]=0;
		   finalBuffer[i]=0;
	   }
	   fread(originBuffer, 1,1, originFile);
	   fread(finalBuffer, 1,1, finalFile);
	   if(originBuffer[0]!=finalBuffer[0])
	   {
	   	ok=false;
	   }
	} while (!feof(originFile) && !feof(finalFile));
   fclose(originFile);
   fclose(finalFile);
   if (ok)
   {
   	printf("Spacecraft: Telemetrías recibidas OK\n");
   }
   else
   {
   	printf("Spacecraft: Telemetrías recibidas ERROR\n");
   }
}



bool_t	CAEmuTMTCServer::EDROOMTopContext::FGetFrames(int32_t numFile,byte_t *pMessage)

 
{
	FILE *fich;
	char rute[50];
	byte_t aux[10];
 
	strcpy(rute,"DwFrames/");
	FIntToChar(numFile,aux);
	strcat(rute,(char *)aux);
	fich=fopen(rute,"rb");    
	if (fich==NULL)
	{
		return false;
	}
	else
	{
		 fread(pMessage,1,50,fich);
		 fclose(fich);
 	}
	return true;
 
 
}



bool_t	CAEmuTMTCServer::EDROOMTopContext::FGetTCPackets(int32_t numeroFichero,byte_t *pMessage)

 
{
   FILE *fichero;
   char ruta[50];
   byte_t aux[10];
 
 
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_MULTI_TC
   strcpy(ruta,"MultiTC/");
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_ONE_TC
   strcpy(ruta,"OneTC/");
#endif
 
   FIntToChar(numeroFichero,aux);
   strcat(ruta,(char *)aux);
 
   fichero=fopen(ruta,"rb");
   if (fichero==NULL)
   {
   	return false;
   }
   else
   {
		fread(pMessage,1,254,fichero);
		fclose(fichero);
 	}
 
  return true;
}



bool_t	CAEmuTMTCServer::EDROOMTopContext::FGetTMPackets(int32_t numeroFichero,byte_t *pMessage)

 
{ 	
	FILE *fichero;
	char ruta[50];
	byte_t aux[10];
 
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_MULTI_TM
   strcpy(ruta,"MultiTM/");
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_ONE_TM
   strcpy(ruta,"OneTM/");
#endif
	FIntToChar(numeroFichero,aux);
	strcat(ruta,(char *)aux);
	fichero=fopen(ruta,"rb");    
	if (fichero==NULL)
	{
	   return false;
	}
	else
	{
		fread(pMessage,1,254,fichero);
		fclose(fichero);
	}
 
	return true;
}



void	CAEmuTMTCServer::EDROOMTopContext::FInit()

 
{
	init_CRC_word16_table();
	FInitFreePackPool();
	FSendInitEmuGS();
}



void	CAEmuTMTCServer::EDROOMTopContext::FInitFreePackPool()

{
	//Inicializamos el Pool para los paquetes
	word16_t numPackagesTM;
	word16_t numPackagesTC;
	word16_t numFreeList;
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_MULTI_TC
    numPackagesTC=125;
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_MULTI_TM
    numPackagesTM=96;
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_ONE_TC
    numPackagesTC=13;
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_ONE_TM
	numPackagesTM=12;
#endif
    numFreeList=32;
	VPackListPool.InitPackagesMemory();
	VPackListPool.AllocPackages(numPackagesTM,VTMPackList);
	VPackListPool.AllocPackages(numPackagesTC,VTCPackList);
 	VPackListPool.AllocPackages(numFreeList,VFreePackList);
	VPackListPool.AllocPackages(numFreeList,VEmuGSFree);
	VPackListPool.AllocPackages(numPackagesTC,VEmuGSTC);//para el EmuGS
 	
}



void	CAEmuTMTCServer::EDROOMTopContext::FInitTCFiles()

 
{
 	bool_t continuar;
	int32_t num=1;
	byte_t newPack[254];
 
	//Inicializar TC con los ficheros
	VEmuGSTC.ToHeadPack();
	if(VEmuGSTC.GetPackCounter())
	{
		do
		{
			CPackHandler newPackHandler;
			VEmuGSTC.GetCurrentEditPack(newPackHandler);
			continuar=FGetTCPackets (num,newPack);
			for (int32_t i=0;i<254;i++)
			{
				newPackHandler.WriteByte(i,newPack[i]);
			}
			VEmuGSTC.WriteCRCCurrentEditPack(0,254);
 
			num++;
		}
		while (continuar && VEmuGSTC.NextEditPack());
	}
 
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FInitTMFiles()

 
{
	byte_t newPack[254];
	int32_t num=1;
	bool_t continuar;
 
	//Inicializar TM con los ficheros
	VTMPackList.ToHeadPack();
	if(VTMPackList.GetPackCounter())
	{
		do
		{
			CPackHandler newPackHandler;
			VTMPackList.GetCurrentEditPack(newPackHandler);
			continuar=FGetTMPackets (num,newPack);
			for (int32_t i=0;i<254;i++)
			{
				newPackHandler.WriteByte(i,newPack[i]);
			}
			VTMPackList.WriteCRCCurrentEditPack(0,254);
			num++;
		}
		while (continuar && VTMPackList.NextEditPack());
	}
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FIntToChar(int32_t num,byte_t * cConvert)

 
{
 
	byte_t aux;
	int32_t i=0;
	int32_t j=0;
	
	do
	{ 
		cConvert[i++]= (num % 10) + '0';
		num=num/10;
	}
	while (num>0);
	cConvert[i--]='\0';
	for (j=0;i>j;i--,j++)
	{
		aux=cConvert[i];
		cConvert[i]=cConvert[j];
		cConvert[j]=aux;
	}
}



void	CAEmuTMTCServer::EDROOMTopContext::FReady()

 
{
// 	printf("TMTC Server: FReady\n");
//	printf("TMTC Server: FReady --> VTMPackList: %d\n",VTMPackList.GetPackCounter());
//	printf("TMTC Server: FReady --> VTCPackList: %d\n",VTCPackList.GetPackCounter());
//	printf("TMTC Server: FReady --> VFreePackList: %d\n",VFreePackList.GetPackCounter());
//	printf("TMTC Server: FReady --> VRouterFree: %d\n",VRouterFree.GetPackCounter());
//	printf("TMTC Server: FReady --> VRouterTC: %d\n",VRouterTC.GetPackCounter());
}
 



void	CAEmuTMTCServer::EDROOMTopContext::FReceiveNextSignal()

 
{
	// send new Msg
	VMoreData=FGetFrames(VMessageNumber,VDataFrame.dataFrame);
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FSendAck()

{
	byte_t radioIdAux[6];
	CDSignal * ap_CDSignal = EDROOMPoolCDSignal.AllocData();
	byte_t finalFrame[17];
 
		int8_t i=0;
		struct ssi_link_layer_signal_frame_info_t ackFrame;
		strcpy((char*)ackFrame.radio_id,"000001");
		ackFrame.sensor_id=MASK_BIDIRECTIONAL_SSI;
		ackFrame.flags=MASK_ACK_SSI;
		strcpy((char*)ackFrame.reserved,"000000");
 
		ssi_link_layer_build_signal_frame(finalFrame,&ackFrame);
 
		for (i=0;i<17;i++)
		{
			ap_CDSignal->signalFrame[i]=finalFrame[i];
		}
 
	// Complete Data
	//build_ack(ap_CDSignal->signalFrame,radioIdAux,MASK_BIDIRECTIONAL_SSI);
	// send new Msg
	StoreAndNextFrame.send(SSignalFrame, EDROOMprioNormal, ap_CDSignal, &EDROOMPoolCDSignal);
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FSendInitEmuGS()

 
 
{
 	CDTMTCCommServer * ap_CDTMTCCommServer = CEDROOMPoolCDTmTcCommServer.AllocData();
	
		// Complete Data 
	
	ap_CDTMTCCommServer->packList1=VEmuGSTC;
	ap_CDTMTCCommServer->packList2=VEmuGSFree;
 
		// send new Msg
	
	SgiTMTCServer2.send(EmuGSDoublePackList, EDROOMprioNormal, ap_CDTMTCCommServer, &CEDROOMPoolCDTmTcCommServer);
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FSendNextData()

{
	CDFrame * ap_CDFrame = EDROOMPoolCDFrame.AllocData();
	*ap_CDFrame=VDataFrame;
	StoreAndNextFrame.send(SDataFrame, EDROOMprioNormal, ap_CDFrame, &EDROOMPoolCDFrame);
	VMessageNumber++;	
}



void	CAEmuTMTCServer::EDROOMTopContext::FSendTCPackList()

 
 
{
 
	CDTMTCCommServer * ap_CDTMTCCommServer = CEDROOMPoolCDTmTcCommServer.AllocData();
	
		// Complete Data 
	ap_CDTMTCCommServer->packList1=VTCPackList;
	//ap_CDTMTCCommServer->packList2=VTCPackList;//TODO: La segunda es redundante
 
		// reply 
	
	Msg->reply(STMTCPackList, ap_CDTMTCCommServer, &CEDROOMPoolCDTmTcCommServer);
 
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FSendTMPackets()

 
{
	word16_t packsNum;
	if (VnumMensajeTM!=0) //TEMPORAL
	{
		CDTMTCCommServer & varCDTMTCCommServer = *(CDTMTCCommServer *)Msg->data;
		packsNum=varCDTMTCCommServer.packList2.GetPackCounter();
		varCDTMTCCommServer.packList2.ExtractPackages(packsNum,VFreePackList);
		VTMPackList=varCDTMTCCommServer.packList1;
	}
	CDTMTCCommServer * ap_CDTMTCCommServer = CEDROOMPoolCDTmTcCommServer.AllocData();
	
		// Complete Data 
 
	ap_CDTMTCCommServer->packList1=VTMPackList;
	ap_CDTMTCCommServer->packList2=VFreePackList;
 
		// reply 
	
	Msg->reply(STMTCPackList, ap_CDTMTCCommServer, &CEDROOMPoolCDTmTcCommServer);
 
 	VnumMensajeTM++;//TEMPORAL
 
}
 



void	CAEmuTMTCServer::EDROOMTopContext::FStoreAck()

{
	//TODO: gestionar los acks recibidos que confirman los datos enviados 
	// descendentemente
}



void	CAEmuTMTCServer::EDROOMTopContext::FStoreFrame()

 
{
	CDFrame & varCDFrame = *(CDFrame *)Msg->data; 
	FILE *fich;
	byte_t data[32];
	TEDROOMInt32 length;
 
	fich=fopen("InterTM.jpg","ab");
	ssi_link_layer_get_data(varCDFrame.dataFrame,data);
	length = ssi_link_layer_get_data_length(varCDFrame.dataFrame);
 
	if (fich != NULL)
	{				
		fwrite(data, 1,length, fich);
		fclose(fich);
	}
 
	if (VMessageReceived==1534)
	{
	   FCheckFiles();
	   images_to_multi_tm_packets();
 
		FInitTMFiles();
 
	 	FInitTCFiles();
 	}
 	VMessageReceived++;
 
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FStoreTCFiles()

 
{
 
	CPackHandler newPackHandler;
	byte_t dataBurst[256];
 
	VTCPackList.ToHeadPack();
	if(VTCPackList.GetPackCounter())
	{
		do
		{
 
			VTCPackList.GetCurrentReadPack(newPackHandler);
			for (int32_t i=0; i<256;i++)
			{
				newPackHandler.ReadByte(i,dataBurst[i]);
			}
			FStoreTCFrame(dataBurst);//TODO:TEMPORAL
 
		}
		while (VTCPackList.NextReadPack());
	}
 
 
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FStoreTCFrame(byte_t * pMessage)

 
{
	char ruta[50];
	byte_t aux[10];
 	FILE *fichero;
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_MULTI_TC
 	strcpy(ruta,"EndMultiTC/");
#endif
#ifdef CONFIG_CAEMUPLANNER_SWR_TEST_ONE_TC
 	strcpy(ruta,"EndOneTC/");
#endif
	FIntToChar(pMessage[0],aux);
	strcat(ruta,(char *)aux);
 	fichero=fopen(ruta,"ab");
	if (fichero!=NULL)
    {
		fwrite(pMessage+2,1,(pMessage[2]+2), fichero);
		fclose(fichero);
	}
 
 
}



void	CAEmuTMTCServer::EDROOMTopContext::FStoreTCPackets()

 
{
 
	CDTMTCCommServer & varCDTMTCCommServer = *(CDTMTCCommServer *)Msg->data;
 
	VTCPackList=varCDTMTCCommServer.packList1;
	VTCPackList=varCDTMTCCommServer.packList2;
 
	FStoreTCFiles();
 
	CDTMTCCommServer * ap_CDTMTCCommServer = CEDROOMPoolCDTmTcCommServer.AllocData();
	// Complete Data 
	ap_CDTMTCCommServer->packList1=VTCPackList;
	ap_CDTMTCCommServer->packList2=VTCPackList;
	
	// reply 
	Msg->reply(STMTCPackList, ap_CDTMTCCommServer, &CEDROOMPoolCDTmTcCommServer);
 
	
 
 
}



bool_t	CAEmuTMTCServer::EDROOMTopContext::GMoreData()

{
 return (VMoreData);
}



void	CAEmuTMTCServer::EDROOMTopContext::images_to_multi_tm_packets()

{
 
	FILE *fichero,*ficheroAuxiliar;
	byte_t aux[10];
	char nombreFichero[40];
	byte_t buffer[254];
	byte_t contadorHex=0x01;
	int i,j;
	word16_t bytesLeidos;
 
	fclose(fopen("Final.jpg","wb"));//para eliminar el archivo destino que existiera
	fichero=fopen("InterTM.jpg","rb");
	if (fichero==NULL)
	{
		////printf("Error al intentar abrir el archivo\n");
	}
	else
	{
		j=1;
		do
		{
			for (i=0;i<254;i++)
			{
				buffer[i]=0;
			}
			for (i=0;i<40;i++)
			{
				nombreFichero[i]=0;
			}
			strcpy(nombreFichero,"MultiTM/");
			FIntToChar(j,aux);
			strcat(nombreFichero,(char*)aux);
			ficheroAuxiliar=fopen(nombreFichero,"wb");
			if (ficheroAuxiliar==NULL)
			{
				////printf("Error al intentar abrir el archivo %d \n",nombreFichero);
			}
			else
			{
				bytesLeidos = fread(buffer, 1,250,fichero);
				#ifdef CONFIG_PLATFORM_IS_LITTLE_ENDIAN
					putc(contadorHex,ficheroAuxiliar);
					putc(0x00,ficheroAuxiliar);
					putc(bytesLeidos,ficheroAuxiliar);
					putc(0x00,ficheroAuxiliar);
				#else
					putc(0x00,ficheroAuxiliar);
					putc(contadorHex,ficheroAuxiliar);
					putc(0x00,ficheroAuxiliar);
					putc(bytesLeidos,ficheroAuxiliar);
				#endif
 
				fwrite(buffer, 1,bytesLeidos, ficheroAuxiliar);
				fclose (ficheroAuxiliar);
			}
			j++;
			contadorHex++;
		}
		while (!feof(fichero));
		fclose(fichero);
	}
 
 
}



	//*****Pools

	//CEDROOMPOOLCDTMTCCommServer

CAEmuTMTCServer::EDROOMTopContext::CEDROOMPOOLCDTMTCCommServer::CEDROOMPOOLCDTMTCCommServer(unsigned int elemCount, CDTMTCCommServer* pMem, TEDROOMBool * pMemMarks):CEDROOMProtectedMemoryPool(elemCount, pMem, pMemMarks, sizeof (CDTMTCCommServer)){;}

CDTMTCCommServer *	CAEmuTMTCServer::EDROOMTopContext::CEDROOMPOOLCDTMTCCommServer::AllocData(){
	return(CDTMTCCommServer*)CEDROOMProtectedMemoryPool::AllocData();
}

	//CEDROOMPOOLCDFrame

CAEmuTMTCServer::EDROOMTopContext::CEDROOMPOOLCDFrame::CEDROOMPOOLCDFrame(unsigned int elemCount, CDFrame* pMem, TEDROOMBool * pMemMarks):CEDROOMProtectedMemoryPool(elemCount, pMem, pMemMarks, sizeof (CDFrame)){;}

CDFrame *	CAEmuTMTCServer::EDROOMTopContext::CEDROOMPOOLCDFrame::AllocData(){
	return(CDFrame*)CEDROOMProtectedMemoryPool::AllocData();
}

	//CEDROOMPOOLCDSignal

CAEmuTMTCServer::EDROOMTopContext::CEDROOMPOOLCDSignal::CEDROOMPOOLCDSignal(unsigned int elemCount, CDSignal* pMem, TEDROOMBool * pMemMarks):CEDROOMProtectedMemoryPool(elemCount, pMem, pMemMarks, sizeof (CDSignal)){;}

CDSignal *	CAEmuTMTCServer::EDROOMTopContext::CEDROOMPOOLCDSignal::AllocData(){
	return(CDSignal*)CEDROOMProtectedMemoryPool::AllocData();
}



// ***********************************************************************

// class EDROOMTopState

// ***********************************************************************



	// CONSTRUCTOR*************************************************

CAEmuTMTCServer::EDROOMTopState::EDROOMTopState (CAEmuTMTCServer &act, CEDROOMMemory *pEDROOMMemory  ):
	  EDROOMTopContext( act , VDataBurst, VDataFrame, VEmuGSFree, VEmuGSTC, VFreePackList, VMessageNumber, VMessageReceived, VMoreData, VnumMensajeTC, VnumMensajeTM, VPackListPool, VTCPackList, VTMPackList,CEDROOMPoolCDTmTcCommServer,EDROOMPoolCDFrame,EDROOMPoolCDSignal )
	, VDataBurst (   )
	, VDataFrame (   )
	, VEmuGSFree (   )
	, VEmuGSTC (   )
	, VFreePackList (   )
	, VMessageNumber ( 1 )
	, VMessageReceived ( 1 )
	, VMoreData ( false )
	, VnumMensajeTC ( 0 )
	, VnumMensajeTM ( 0 )
	, VPackListPool (   )
	, VTCPackList (   )
	, VTMPackList (   )
	, CEDROOMPoolCDTmTcCommServer ( 3, pEDROOMMemory->poolCDTMTCCommServer, pEDROOMMemory->poolMarkCDTMTCCommServer)
	, EDROOMPoolCDFrame ( 10, pEDROOMMemory->poolCDFrame, pEDROOMMemory->poolMarkCDFrame)
	, EDROOMPoolCDSignal ( 10, pEDROOMMemory->poolCDSignal, pEDROOMMemory->poolMarkCDSignal)
{

}

	// EDROOMBehaviour**********************************************

void CAEmuTMTCServer::EDROOMTopState::EDROOMBehaviour(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans = EDROOMIllegada();

	do{

		switch(edroomCurrentTrans.localId){

			case(SendTMPacks):
				FSendTMPackets();
				edroomNextState = Ready;
				break;
			case(Init):
				FInit();
				edroomNextState = Ready;
				break;
			case(ReceiveTCPacks):
				FStoreTCPackets();
				edroomNextState = Ready;
				break;
			case(SendTCPackList):
				FSendTCPackList();
				edroomNextState = Ready;
				break;
			case(StoreFrame):
				FStoreFrame();
				edroomNextState = Ready;
				break;
			case(ReceiveAck):
				FStoreAck();
				edroomNextState = Ready;
				break;
			case(ReceiveNextSignal):
				FReceiveNextSignal();

				EDROOMcomponent.EDROOMTraceStateEntry();

				if( GMoreData() ){
					FSendNextData();
					edroomCurrentTrans.localId = ReceiveNextSignal_SendData;
					edroomNextState = Ready;
				 } 
				else{
					FSendAck();
					edroomCurrentTrans.localId = ReceiveNextSignal_SendAck;
					edroomNextState = Ready;
				 } 
				EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);
				break;
		}

		EDROOMcomponent.EDROOMTraceStateEntry();

		EDROOMcomponent.EDROOMTraceStateEntry();

		switch(edroomNextState){

			case(I):
				edroomCurrentTrans=EDROOMIllegada();
				break;

			case(Ready):
				FReady();
				edroomCurrentTrans=EDROOMReadyllegada();
				break;

		}

		EDROOMcomponent.EDROOMTraceStateExit();

		edroomCurrentState=edroomNextState;

	}while(Msg->signal != EDROOMSignalDestroy);

}



	// Inicio**********************************************

void CAEmuTMTCServer::EDROOMTopState::EDROOMInicio(){

edroomCurrentState=I;

}



//	 ***********************************************************************

//	 llegada SubEstado I

//	 ***********************************************************************



TEDROOMTransId CAEmuTMTCServer::EDROOMTopState::EDROOMIllegada(){

	TEDROOMTransId edroomCurrentTrans;

	edroomCurrentTrans.localId = Init;
	edroomCurrentTrans.distanceToContext = 0;
	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, 0);

	return(edroomCurrentTrans);

}



	// ***********************************************************************

	// llegada SubEstado Hoja Ready

	// ***********************************************************************



TEDROOMTransId CAEmuTMTCServer::EDROOMTopState::EDROOMReadyllegada(){

	TEDROOMTransId edroomCurrentTrans;

	int edroomValidMsg=false;

	do{

		EDROOMNuevoMens ();

		switch(Msg->signal){

			case (STMDoublePackList): 

				 if ( *Msg->GetPInterface() == SgiTMTCServer) {

					edroomCurrentTrans.localId= SendTMPacks;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (SFinalTCPackets): 

				 if ( *Msg->GetPInterface() == SgiTMTCServer) {

					edroomCurrentTrans.localId= ReceiveTCPacks;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (STCPackList): 

				 if ( *Msg->GetPInterface() == SgiTMTCServer) {

					edroomCurrentTrans.localId= SendTCPackList;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (SDataFrame): 

				 if ( *Msg->GetPInterface() == StoreAndNextFrame) {

					edroomCurrentTrans.localId= StoreFrame;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (SSignalFrame): 

				 if ( *Msg->GetPInterface() == StoreAndNextFrame) {

					edroomCurrentTrans.localId= ReceiveAck;
					edroomCurrentTrans.distanceToContext = 0;
					edroomValidMsg=true;
				 }

				break;

			case (SNextSensorSsiDataFrame): 

				 if ( *Msg->GetPInterface() == StoreAndNextFrame) {

					edroomCurrentTrans.localId = ReceiveNextSignal;
					edroomCurrentTrans.distanceToContext = 0 ;
					edroomValidMsg=true;
				 }

				break;

		};

		if (!edroomValidMsg) edroomValidMsg = EDROOMSearchContextTrans(edroomCurrentTrans);

	} while (!edroomValidMsg);

	EDROOMcomponent.EDROOMTraceTrans(edroomCurrentTrans, Msg->signal);

	return(edroomCurrentTrans);

}



